package com.lzj.jdbc;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class HelloHBaseDML {
    //获取HBase管理员类
    private Admin admin;
    //获取数据库连接
    private Connection connection;
    //获取表信息
    private Table table;

    /**
     * 像数据库添加数据
     *
     * @throws IOException
     */
    @Test
    public void putOneRowData() throws IOException {

        Put put = new Put(Bytes.toBytes("s2020111701"));
        put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("lg"));
        put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes(16));
        put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("sex"), Bytes.toBytes("male"));
        put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("money"), Bytes.toBytes(2000.0));

        put.addColumn(Bytes.toBytes("score"), Bytes.toBytes("Chinese"), Bytes.toBytes(98));
        put.addColumn(Bytes.toBytes("score"), Bytes.toBytes("English"), Bytes.toBytes(96));
        put.addColumn(Bytes.toBytes("score"), Bytes.toBytes("Math"), Bytes.toBytes(95));

        table.put(put);
        table.close();
    }

    /**
     * 向数据库添加数据
     *
     * @throws IOException
     */
    @Test
    public void putMoreRowData() throws IOException {
        List<Put> puts = new ArrayList<>();
        for (int i = 0; i < 1000; i++) {
            Put put = new Put(Bytes.toBytes("s205012" + i));
            put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("name" + i));
            put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes(i % 10 + 18));
            put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("sex"), Bytes.toBytes(i % 9 == 0 ? "male" : "female"));
            put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("money"), Bytes.toBytes(i % 90 + 10));

            put.addColumn(Bytes.toBytes("score"), Bytes.toBytes("Chinese"), Bytes.toBytes(i % 100));
            put.addColumn(Bytes.toBytes("score"), Bytes.toBytes("English"), Bytes.toBytes(i % 100));
            put.addColumn(Bytes.toBytes("score"), Bytes.toBytes("Math"), Bytes.toBytes(i % 100));

            puts.add(put);
        }
        table.put(puts);
        table.close();
    }

    /**
     * 删除行信息
     *
     * @throws IOException
     */
    @Test
    public void deleteRowData() throws IOException {
        Delete delete = new Delete("s2020111701".getBytes());
        table.delete(delete);
        System.out.println("删除数据！s2020111701");
    }

    @Before
    public void init() throws IOException {
        Configuration configuration = HBaseConfiguration.create();
        this.connection = ConnectionFactory.createConnection(configuration);
        this.admin = connection.getAdmin();
        //获取数据库表信息
        table = this.connection.getTable(TableName.valueOf("lzj:student"));
    }

    @After
    public void destory() throws IOException {
        if (admin != null) {
            admin.close();
        }
        if (table != null) {
            table.close();
        }
        if (connection != null) {
            connection.close();
        }
    }

}
